//
//  HBSignCell.m
//  HeartBeat
//
//  Created by karong on 2023/8/29.
//

#import "HBSignCell.h"
#import "HBSignItemCell.h"
#import "HBSignModel.h"
@interface HBSignCell () <HBSignItemCellDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) NSArray * dataArray;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *myCoinsLabel;
@property (nonatomic, strong) HBSign * object;
@end

@implementation HBSignCell
- (void)awakeFromNib {
    [super awakeFromNib];
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setMycoins) name:WSRefreshUserInfoNotification object:nil];
}

- (void)setDataWithObject:(HBSign *)object {
    self.object = object;
    self.dataArray = object.items;
    [self.listView reloadData];
    [self layoutTitleLabelWithObject:object];
    [self setMycoins];
    [self.listView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.object.sign_days inSection:0] atScrollPosition:(UICollectionViewScrollPositionCenteredHorizontally) animated:NO];
}

- (void)layoutTitleLabelWithObject:(HBSign *)object {
    NSString *text = SF(WSLocalized(@"連續簽到%ld天"), (long)object.sign_days);
    NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:text
                                                                                 attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14], NSForegroundColorAttributeName: UIColor.font_black_1A1A1A}];
    
    NSString *string1 = SF(@"%ld", (long)object.sign_days);
    NSRange range1 = [text rangeOfString:string1];
    [attrText addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:18 weight:(UIFontWeightBold)], NSForegroundColorAttributeName:[UIColor colorFromHexRGB:@"FF4700"]} range:range1];
    self.titleLabel.attributedText = attrText;
}

- (void)setMycoins {
    self.myCoinsLabel.text = SF(@"%ld", (long)appUserCenter.coins);
}

- (IBAction)roleAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(signCellRoleAction)]) {
        [self.delegate signCellRoleAction];
    }
}

#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HBSignItemCell * cell;
    HBSignModel * sign = self.dataArray[indexPath.row];
    if (self.object.is_sign_today == 0 && indexPath.row == self.object.sign_days) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBSignTodayCell" forIndexPath:indexPath];
        cell.delegate = self;
    }
    else {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBSignItemCell" forIndexPath:indexPath];
    }
    [cell setDataWithObject:sign day:self.object.sign_days];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = floor((HBScreenWidth - 12 - 12 - 10 - 10) / 7.0f);
    size.height = 105;
    return size;
}

#pragma mark HBSignItemCellDelegate
- (void)signItemCellSignAction:(UICollectionViewCell *)cell {
    if ([self.delegate respondsToSelector:@selector(signCellSignAction)]) {
        [self.delegate signCellSignAction];
    }
}
@end
